package com.pointmi.loyaltydao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

@Transactional
public class LoyaltyDAOImpl implements LoyaltyDAO {

	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	protected final Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}
	@SuppressWarnings("rawtypes")
	public Loyalty checkLoyalty(String customerId) {
		Query query = getCurrentSession().createQuery(
				"from Loyalty where customerId = :customerId");
		query.setParameter("customerId", customerId);
		List list = query.list();
		return foundLoyalty(list);
	}

	@SuppressWarnings("rawtypes")
	private Loyalty foundLoyalty(List list) {
		Loyalty loyalty = null;
		if (list.size() > 0) {
			loyalty = (Loyalty) list.get(0);
		}
		return loyalty;
	}
	
	public void saveOrUpdate(Loyalty newLoyalty) {
		getCurrentSession().saveOrUpdate(newLoyalty);
	}


}
